﻿#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <qqmlcontext.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
#include <model/filemodel.h>
#include <model/filetree.h>
#include <util/apputil.h> // 全局参数
#include <util/fileutil.h>
#include <util/listutil.h>
#include <view/mainview.h>
#include <view/menuview.h>
#include <btnclick.h>
#include <QtWebEngine/qtwebengineglobal.h>

using namespace std;

int main(int argc, char *argv[]) {

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    qmlRegisterType<BtnClick>("cus.BtnClick", 1, 0, "BtnClick");
    qmlRegisterType<MainView>("view.MainView", 1, 0, "MainView");
    qmlRegisterType<MenuView>("view.MenuView", 1, 0, "MenuView");


    // QtWebEngine::initialize(); // 初始化 webengine

    // 添加文件
    if (FileUtil::exists(AppUtil::location)) {
        FileTreeService* ser;
        AppUtil::fileTreeRoot = ser->getAll(AppUtil::location);
        foreach(FileModel* file, AppUtil::fileTreeRoot->childs) {
            file->visible = true; // 第一层显示
        }
        QList<FileModel*> fileList = ser->getList(AppUtil::fileTreeRoot); // 获取文件列表
        ListUtil::add(AppUtil::dataList, ListUtil::cast<FileModel*, QObject*>(fileList)); // 添加到视图

        ser = nullptr;
    } else {
        AppUtil::dataList = {};
    }

    // 设置属性
    engine.rootContext()->setContextProperty("myModel", QVariant::fromValue(AppUtil::dataList));
    engine.rootContext()->setContextProperty("listHeight", QVariant::fromValue(AppUtil::dataList.size() * 15));


    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    AppUtil::ENGINE = &engine; // 存储指针

    return app.exec();
}
